/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.solution;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import model.entity.Customerdemand;
import model.entity.Depot;
import model.entity.Problem;
import model.entity.Solution;
import model.entity.Task;
import model.entity.TaskCustomerdemand;
import org.omnifaces.util.Faces;
import service.InterfaceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Named(value = "pointsMB")
@SessionScoped
public class PointsMB implements Serializable {

    String order;
    String points;
    String depotPoints;
    String cdStr;
//    List< Problem> pl;
    List< Solution> sl;
    Problem p;
    Solution solution;
    List<Task> tasklist;//已选择的任务
    @EJB(beanName = "ProblemManager")
    private InterfaceServiceLocal<Problem> problemManager;
    @EJB(beanName = "SolutionManager")
    private InterfaceServiceLocal<Solution> solutionManager;
    String idproblem;
    String idsolution;
    String idproblemparam;
    String idsolutionparam;
    String linesArray = "";
    boolean render = true;
    boolean rendersolution = true;

    /**
     * Creates a new instance of PointsMB
     */
    public PointsMB() {

    }

    public void prerender() {

        System.out.println("idproblem:" + idproblem);
        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }
        idsolution = idsolutionparam;
        idproblem = idproblemparam;
        idproblemparam = null;
        idsolutionparam = null;
        init();

    }
//    @PostConstruct

    public void init() {
        if (idsolution != null) {
            rendersolution = false;
            solution = solutionManager.find(Integer.parseInt(idsolution));
            p = solution.getIdproblem();
            tasklist = new ArrayList(solution.getTaskList());
        } else {

            if (idproblem == null || idproblem.equals("")) {
                render = false;
                sl = null;
                solution = null;
                tasklist = null;
                return;
            }
            //检查是否为Int
            try {
                Integer.parseInt(idproblem);
            } catch (Exception e) {
                System.out.println("idproblem is not num");
                render = false;
                return;

            }

            p = problemManager.find(Integer.parseInt(idproblem));

            sl = p.getSolutionList();

            if (sl == null || sl.isEmpty()) {
                tasklist = new ArrayList();
                solution = null;
                return;
            } else {
                solution = sl.get(0);
                tasklist = new ArrayList(solution.getTaskList());

            }
            rendersolution = true;
            render = true;
            System.out.println("PointsMB init");
        }

    }

    public void onproblemChange() {
        p = problemManager.find(Integer.parseInt(idproblem));
        sl = p.getSolutionList();
    }

    public void onsubmit() {
        solution = solutionManager.find(Integer.parseInt(idsolution));
        tasklist = new ArrayList(solution.getTaskList());
    }

    public void submittask() {
        System.out.println("task:" + this.tasklist);
    }
 

    public String getPoints() {
        System.out.println("getPoints");
        points = "";
        order = "";
        cdStr = "";
        if (solution == null || solution.getTaskList() == null || solution.getTaskList().isEmpty() || tasklist.isEmpty()) {
            return points;
        };

        List<Task> tl = this.tasklist;
DateFormat df = DateFormat.getTimeInstance();//只显示出时分秒
        for (Task task : tl) {
            List<Customerdemand> cl = new ArrayList();
            for (TaskCustomerdemand cd : task.getTaskCustomerdemandList()) {
                Customerdemand customerdemand = cd.getIdcustomerdemand();
                cl.add(customerdemand);
                order += "'车辆:" + task.getIdattendance().getIdvehicle().getPlate() + ",顺序:" + cd.getVisiteorder() + "',";
                cdStr += "'订单id:" + customerdemand.getIdcustomerdemand()
                        + "<br />地址:" + customerdemand.getAddress()
                        + "<br />电话:" + customerdemand.getTel()
                        + "<br />需求量:" + customerdemand.getRequirement()+"KG"
                        + "<br />服务时间:" + customerdemand.getServiceTime()+"分钟"
                        + "<br />最早收货时间:" + df.format(customerdemand.getReadyTime())
                        + "<br />最晚收货时间:" + df.format(customerdemand.getDueDate())
                        + "<br />预计到达:" + df.format(cd.getArrivetime())
                        +"',";

            }
            System.out.println(cl);

            for (Customerdemand cd : cl) {
                System.out.println(cd);
                points += getPointsFromCustomerdemand(cd) + ",";

            }
        }

        points = points.substring(0, points.length() - 1);
        order = order.substring(0, order.length() - 1);
        cdStr = cdStr.substring(0, cdStr.length() - 1);
        return points;
    }

    public String getDepotPoints() {
        System.out.println("getDepotPoints");

        this.depotPoints = "";
        if (p == null) {
            return depotPoints;
        }
        this.depotPoints += getPointsFromDepot(p.getIddepot()) + ",";

        depotPoints = depotPoints.substring(0, depotPoints.length() - 1);

        return depotPoints;
    }

//    public String getPointsFromNode(Node node) {
//        String point = "";
////        System.out.println("getPointsFromNode:" + node.getLongitude());
//        point += "new BMap.Point(" + node.getLongitude() + "," + node.getLatitude() + ")";
//        return point;
//    }
    public String getPointsFromDepot(Depot depot) {
        String point = "";
//        System.out.println("getPointsFromNode:" + node.getLongitude());
        point += "new BMap.Point(" + depot.getLongitude() + "," + depot.getLatitude() + ")";
        return point;
    }

    public String getPointsFromCustomerdemand(Customerdemand customerdemand) {
        String point = "";
//        System.out.println("getPointsFromNode:" + node.getLongitude());
        point += "new BMap.Point(" + customerdemand.getLongitude() + "," + customerdemand.getLatitude() + ")";
        return point;
    }

    public void setPoints(String points) {
        this.points = points;
    }

    public void setDepotPoints(String depotPoints) {
        this.depotPoints = depotPoints;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public Problem getP() {
        return p;
    }

    public void setP(Problem p) {
        this.p = p;
    }

//    public List<Problem> getPl() {
//        return pl;
//    }
//
//    public void setPl(List<Problem> pl) {
//        this.pl = pl;
//    }
    public String getIdproblem() {
        return idproblem;
    }

    public void setIdproblem(String idproblem) {
        this.idproblem = idproblem;
    }

    public String getIdsolution() {
        return idsolution;
    }

    public void setIdsolution(String idsolution) {
        this.idsolution = idsolution;
    }

    public List<Solution> getSl() {
        return sl;
    }

    public void setSl(List<Solution> sl) {
        this.sl = sl;
    }

    public String getLinesArray() {
        System.out.println("p:" + p);
        linesArray = "";
//        if (!"".equals(linesArray)) {
//            return linesArray;
//        }
        if (solution == null || solution.getTaskList() == null || solution.getTaskList().isEmpty() || tasklist.isEmpty()) {
            return linesArray;
        };

        List< Task> tl = this.tasklist;
        for (Task task : tl) {
            String strokeColor = getRandColor();
            linesArray += "new BMap.Polyline(  " + task.getDirectionstring();
            linesArray += ", {strokeColor:'" + strokeColor + "', strokeWeight:5, strokeOpacity:0.9}),";
        }
//        for (int t = 0; t < p.getTaskCollection().size(); t++) {
//            entities.Task tasklist = tl.get(t);
//
//            String strokeColor = getRandColor();
//            linesArray += "new BMap.Polyline(  " + tasklist.getDirectionstring();
//            linesArray += ", {strokeColor:'" + strokeColor + "', strokeWeight:3, strokeOpacity:0.7}),";
//
//        }

        linesArray = linesArray.substring(0, linesArray.length() - 1);

        return linesArray;
    }

    public void setLinesArray(String linesArray) {
        this.linesArray = linesArray;
    }

    public String getRandColor() {
        String s = "#";
        for (int i = 0; i < 3; i++) {
            s += Integer.toHexString((int) (Math.random() * (15 + 1)));//1-15,避免白色
        }
        System.out.println(s);
        return s;

    }

    public boolean isRender() {
        return render;
    }

    public void setRender(boolean render) {
        this.render = render;
    }

    public Solution getSolution() {
        return solution;
    }

    public void setSolution(Solution solution) {
        this.solution = solution;
    }

    public List<Task> getTasklist() {
        return tasklist;
    }

    public void setTasklist(List<Task> tasklist) {
        this.tasklist = tasklist;
    }

    public String getIdproblemparam() {
        return idproblemparam;
    }

    public void setIdproblemparam(String idproblemparam) {
        this.idproblemparam = idproblemparam;
    }

    public String getIdsolutionparam() {
        return idsolutionparam;
    }

    public void setIdsolutionparam(String idsolutionparam) {
        this.idsolutionparam = idsolutionparam;
    }

    public boolean isRendersolution() {
        return rendersolution;
    }

    public void setRendersolution(boolean rendersolution) {
        this.rendersolution = rendersolution;
    }

    public String getCdStr() {
        return cdStr;
    }

    public void setCdStr(String cdStr) {
        this.cdStr = cdStr;
    }
    

}
